package com.verisette.ncos.service.card.bizunit;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.common.MailConstant;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.web.card.bean.CardCreateBean;

public class SendMailCreateCardBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(SendMailCreateCardBU.class);
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		// TODO Auto-generated method stub
		
		MasterUserProfile userBean = (MasterUserProfile)processContext.getUserBean();
		
		CardCreateBean cardCreateBean = (CardCreateBean)processContext.getOriginalRequest().getValueObject();
		MasterMember member = cardCreateBean.getOrder().getMasterMember();
		member.getMmbEmail();
		SendMailThread sendMailThread = new SendMailThread();
		sendMailThread.setCardCreateBean(cardCreateBean);
		sendMailThread.setUserBean(userBean);
		
		sendMailThread.start();
	}
	
	class SendMailThread extends Thread {
		private UserBean userBean;
		private CardCreateBean cardCreateBean;
		@Override
		public void run() {
			// TODO Auto-generated method stub
			MasterMember member = cardCreateBean.getOrder().getMasterMember();
			
			if(logger.isDebugEnabled()) {
				logger.debug("###### SMTP_USER : " + MailConstant.SMTP_USER);
				logger.debug("###### SMTP_PASSWORD : " + MailConstant.SMTP_PASSWORD);
			}
			
			Session session = Session.getInstance(MailConstant.SMTP_PROPS,
					  new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(MailConstant.SMTP_USER, MailConstant.SMTP_PASSWORD);
						}
					  });
			
			try {
				MimeMessage message = new MimeMessage(session);
				message.setFrom(new InternetAddress(MailConstant.SMTP_USER));
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(member.getMmbEmail()));
				if( StringUtils.isBlank(member.getMmbFnameTh())){
					message.setSubject(String.format("ระบบนามบัตรออนไลน์ : มีนามบัตรจากคุณ %s %s รอการอนุมัติ", member.getMmbFnameTh(), member.getMmbLnameTh()), "UTF-8");
				} else {
					message.setSubject(String.format("ระบบนามบัตรออนไลน์ : มีนามบัตรจากคุณ %s %s รอการอนุมัติ", member.getMmbFnameEn(), member.getMmbLnameEn()), "UTF-8");
				}
				
				String name = "ไม่ทราบชื่อ";
				if(StringUtils.isNotBlank(userBean.getFirstName())){
					name = userBean.getFirstName();
					if(StringUtils.isNotBlank(userBean.getLastName())){
						name = name + " " + userBean.getLastName();
					}
				}
				
				message.setContent(
//						String.format(MailConstant.mailTemplateMap.get("mail-card-request"),
						String.format(MailConstant.mailTemplateMap.get("mail-approve-request"),
								name, member.getMmbFnameTh() + " " + member.getMmbLnameTh()), 
						"text/html;charset=UTF-8;");
//				message.setText(String.format("เรียนคุณ %1$s,"
//					+ "\n\n กรุณาพิจารณานามบัตรของคุณ %2$s จากระบบ NCOS ครับ \n\nขอแสดงความนับถือ", member.getMmbFnameTh() + " " + member.getMmbLnameTh(), name ));
				
				Transport.send(message);
				
			} catch (MessagingException e) {
				throw new RuntimeException(e);
			}
		}
		public void setUserBean(UserBean userBean) {
			this.userBean = userBean;
		}
		public void setCardCreateBean(CardCreateBean cardCreateBean) {
			this.cardCreateBean = cardCreateBean;
		}
	}
}